package com.anquing.securitydemo.server;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangqq65
 * @since 2021/8/17 9:37
 */
@Service
public class UserDetailsServiceImpl  implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // todo 真实场景是从数据库查询，支持用户名或手机号查询
        // 如果使用User.builder()的方式创建，要注意password要使用的是未加密过的，因为build()方法中，会进行加密
        // 真实场景中，很少使用角色去做权限认证

        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER", "TEST");
        // password=123456
        return new User(username,"$2a$10$DiBodxzMwHfBXP1.8s9WTOg0VuwXoTejcYB2duwQS6MFXX9vUsV0K",authorityList);
    }

}
